package bb.lanxing.model.json;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.baidu.mapapi.model.LatLng;
import com.github.mikephil.charting.utils.Utils;

import android.os.Parcel;
import android.os.Parcelable;

import bb.lanxing.util.PolyUtil;
import bb.lanxing.util.map.SphericalUtil;

@JSONType(ignores = { "startInstruction", "endInstruction" })
public class RouteStep implements Parcelable, Serializable {
    public static final Creator<RouteStep> CREATOR = new Creator<RouteStep>() {
        @Override
        public RouteStep createFromParcel(Parcel parcel) {
            return new RouteStep(parcel);
        }

        @Override
        public RouteStep[] newArray(int i) {
            return new RouteStep[i];
        }
    };
    public static final String MANEUVER_FORK_LEFT = "fork-left";
    public static final String MANEUVER_FORK_RIGHT = "fork-right";
    public static final String MANEUVER_LEFT = "turn-left";
    public static final String MANEUVER_MERGE = "merge";
    public static final String MANEUVER_RAMP_LEFT = "ramp-left";
    public static final String MANEUVER_RAMP_RIGHT = "ramp-right";
    public static final String MANEUVER_RIGHT = "turn-right";
    public static final String MANEUVER_ROUNDABOUT_LEFT = "roundabout-left";
    public static final String MANEUVER_ROUNDABOUT_RIGHT = "roundabout-right";
    public static final String MANEUVER_SHARP_LEFT = "turn-sharp-left";
    public static final String MANEUVER_SHARP_RIGHT = "turn-sharp-right";
    public static final String MANEUVER_SLIGHT_LEFT = "turn-slight-left";
    public static final String MANEUVER_SLIGHT_RIGHT = "turn-slight-right";
    public static final String MANEUVER_STRAIGHT = "straight";
    public static final String MANEUVER_UTURN_LEFT = "uturn-left";
    public static final String MANEUVER_UTURN_RIGHT = "uturn-right";
    private IntValuePair distance;
    private IntValuePair duration;
    private String endInstruction;
    @JSONField(name = "end_location")
    private RoutePoint endLoc;
    @JSONField(name = "html_instructions")
    private String htmlInstruction;
    private String maneuver;
    @JSONField(serialize = false)
    private List<LatLng> polyPath;
    private RoutePolyline polyline;
    private String startInstruction;
    @JSONField(name = "start_location")
    private RoutePoint startLoc;
    @JSONField(name = "travel_mode")
    private String travelModel;

    public RouteStep() {
    }

    protected RouteStep(Parcel parcel) {
        this.distance = (IntValuePair) parcel.readParcelable(IntValuePair.class.getClassLoader());
        this.duration = (IntValuePair) parcel.readParcelable(IntValuePair.class.getClassLoader());
        this.startLoc = (RoutePoint) parcel.readParcelable(RoutePoint.class.getClassLoader());
        this.endLoc = (RoutePoint) parcel.readParcelable(RoutePoint.class.getClassLoader());
        this.htmlInstruction = parcel.readString();
        this.polyline = (RoutePolyline) parcel.readParcelable(RoutePolyline.class.getClassLoader());
        this.polyPath = parcel.createTypedArrayList(LatLng.CREATOR);
        this.travelModel = parcel.readString();
        this.maneuver = parcel.readString();
        this.startInstruction = parcel.readString();
        this.endInstruction = parcel.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public int calMinDistance(LatLng latLng) {
        getPolyPath();
        int i = Integer.MAX_VALUE;
        if (this.polyPath.isEmpty()) {
            return Integer.MAX_VALUE;
        }
        if (this.polyPath.size() == 1) {
            return (int) SphericalUtil.computeDistanceBetween(latLng, this.polyPath.get(0));
        }
        for (int i2 = 1; i2 < this.polyPath.size(); i2++) {
            int i3 = i2 - 1;
            double distanceToLine = PolyUtil.distanceToLine(latLng,
                    new LatLng(this.polyPath.get(i3).latitude, this.polyPath.get(i3).longitude),
                    new LatLng(this.polyPath.get(i2).latitude, this.polyPath.get(i2).longitude));
            if (distanceToLine < i) {
                i = (int) distanceToLine;
            }
        }
        return i;
    }

    public List<LatLng> getPolyPath() {
        if (this.polyPath == null) {
            RoutePolyline routePolyline = this.polyline;
            if (routePolyline == null) {
                this.polyPath = new ArrayList(0);
            } else {
                this.polyPath = routePolyline.getPolyPath();
            }
        }
        return this.polyPath;
    }

    public void setPolyPath(List<LatLng> list) {
        this.polyPath = list;
    }

    public IntValuePair getDistance() {
        if (this.distance == null) {
            this.distance = new IntValuePair();
            if (this.polyPath != null) {
                double d = Utils.DOUBLE_EPSILON;
                for (int i = 1; i < this.polyPath.size(); i++) {
                    d += SphericalUtil.computeDistanceBetween(this.polyPath.get(i - 1), this.polyPath.get(i));
                }
                int round = (int) Math.round(d);
                String str = round > 1000 ? (round / 1000) + " kilometre" : round + "  meter";
                this.distance.setValue(round);
                this.distance.setText(str);
            }
        }
        return this.distance;
    }

    public void setDistance(IntValuePair intValuePair) {
        this.distance = intValuePair;
    }

    public IntValuePair getDuration() {
        return this.duration;
    }

    public void setDuration(IntValuePair intValuePair) {
        this.duration = intValuePair;
    }

    public RoutePoint getStartLoc() {
        return this.startLoc;
    }

    public void setStartLoc(RoutePoint routePoint) {
        this.startLoc = routePoint;
    }

    public RoutePoint getEndLoc() {
        return this.endLoc;
    }

    public void setEndLoc(RoutePoint routePoint) {
        this.endLoc = routePoint;
    }

    public String getHtmlInstruction() {
        return this.htmlInstruction;
    }

    public void setHtmlInstruction(String str) {
        this.htmlInstruction = str;
    }

    public RoutePolyline getPolyline() {
        return this.polyline;
    }

    public void setPolyline(RoutePolyline routePolyline) {
        this.polyline = routePolyline;
    }

    public String getTravelModel() {
        return this.travelModel;
    }

    public void setTravelModel(String str) {
        this.travelModel = str;
    }

    public String getManeuver() {
        return this.maneuver;
    }

    public void setManeuver(String str) {
        this.maneuver = str;
    }

    public String getStartInstruction() {
        return this.startInstruction;
    }

    public void setStartInstruction(String str) {
        this.startInstruction = str;
    }

    public String getEndInstruction() {
        return this.endInstruction;
    }

    public void setEndInstruction(String str) {
        this.endInstruction = str;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeParcelable(this.distance, i);
        parcel.writeParcelable(this.duration, i);
        parcel.writeParcelable(this.startLoc, i);
        parcel.writeParcelable(this.endLoc, i);
        parcel.writeString(this.htmlInstruction);
        parcel.writeParcelable(this.polyline, i);
        parcel.writeTypedList(this.polyPath);
        parcel.writeString(this.travelModel);
        parcel.writeString(this.maneuver);
        parcel.writeString(this.startInstruction);
        parcel.writeString(this.endInstruction);
    }

    public static class IntValuePair implements Parcelable, Serializable {
        public static final Creator<IntValuePair> CREATOR = new Creator<IntValuePair>() {
            @Override
            public IntValuePair createFromParcel(Parcel parcel) {
                return new IntValuePair(parcel);
            }

            @Override
            public IntValuePair[] newArray(int i) {
                return new IntValuePair[i];
            }
        };
        String text;
        int value;

        public IntValuePair() {
        }

        public IntValuePair(int i, String str) {
            this.value = i;
            this.text = str;
        }

        protected IntValuePair(Parcel parcel) {
            this.value = parcel.readInt();
            this.text = parcel.readString();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        public int getValue() {
            return this.value;
        }

        public void setValue(int i) {
            this.value = i;
        }

        public String getText() {
            return this.text;
        }

        public void setText(String str) {
            this.text = str;
        }

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeInt(this.value);
            parcel.writeString(this.text);
        }
    }
}
